package com.example.flybird.activity;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Gravity;
import android.widget.*;
import androidx.annotation.Nullable;

import com.example.flybird.R;
import com.example.flybird.dao.LeaderboardDAO;
import com.example.flybird.model.Leaderboard;
import java.util.List;


public class PersonalRecord extends Activity {
    TableLayout tableLayout;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.personal_record);
        tableLayout=(TableLayout) findViewById(R.id.pRecord);
        init(tableLayout);
        final LeaderboardDAO leaderboardDAO = new LeaderboardDAO(this);
        final List<Leaderboard> list = leaderboardDAO.findAll();
        add(list,tableLayout);
    }


    private void init(TableLayout tableLayout){
        //创建一行
        TableRow row = new TableRow(getApplicationContext());

        //创建显示的内容,这里创建的是一列
        TextView text1 = new TextView(getApplicationContext());
        text1.setPadding(0,0,150,0);
        TextView text2 = new TextView(getApplicationContext());
        TextView text3 = new TextView(getApplicationContext());

        //设置显示内容
        text1.setText("游戏名");
        text2.setText("成绩");
        text2.setPadding(0,0,150,0);
        text3.setText("时间");

        text1.setTextColor(Color.BLUE);
        text2.setTextColor(Color.BLUE);
        text3.setTextColor(Color.BLUE);

        text1.setTextSize(20);
        text2.setTextSize(20);
        text3.setTextSize(20);

        text1.setGravity(Gravity.CENTER);
        text2.setGravity(Gravity.CENTER);
        text3.setGravity(Gravity.CENTER);

        //添加到Row
        row.addView(text1);
        row.addView(text2);
        row.addView(text3);

        tableLayout.addView(row);
    }

    public  void add(List<Leaderboard> list, TableLayout tableLayout){
        for (Leaderboard l :
                list) {
            TableRow row = new TableRow(getApplicationContext());

            //创建显示的内容,这里创建的是一列
            TextView text1 = new TextView(getApplicationContext());
            text1.setPadding(0,0,150,0);

            TextView text2 = new TextView(getApplicationContext());
            text2.setPadding(0,0,150,0);

            TextView text3 = new TextView(getApplicationContext());

            text1.setGravity(Gravity.CENTER);
            text2.setGravity(Gravity.CENTER);
            text3.setGravity(Gravity.CENTER);

            text1.setTextSize(16);
            text2.setTextSize(16);
            text3.setTextSize(16);

            text1.setTextColor(Color.BLACK);
            text2.setTextColor(Color.BLACK);
            text3.setTextColor(Color.BLACK);

            text1.setText(l.getName());
            text2.setText(String.valueOf(l.getIntegral()));
            text3.setText(l.getDate());

            row.addView(text1);
            row.addView(text2);
            row.addView(text3);
            tableLayout.addView(row);
        }
    }
}
